package com.occupation_health.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;

import com.jfinal.core.Controller;
import com.occupation_health.model.UserApi;

public class UserController extends BaseController {
	public void index() {
		this.login();
	}

	/*
	 * 登陆页面
	 */
	public void login() {
		if (this.getRequest().getMethod() == "POST") {
			UserApi user = getModel(UserApi.class);
			UsernamePasswordToken token = new UsernamePasswordToken(
					user.getStr("username"), user.getStr("password"));
			if (getPara("remember-me") != null)
				token.setRememberMe(true);
			Subject currentUser = SecurityUtils.getSubject();
			try {
				currentUser.login(token);
				this.redirect("/hello");
			} catch (AuthenticationException ae) {
				this.setAttr("error", ae.getLocalizedMessage());
				this.render("login.html");
			}

		} else {
			render("login.html");
		}

	}

	/*
	 * 注销登陆
	 */
	public void logout() {
		Subject currentUser = SecurityUtils.getSubject();
		try {
			currentUser.logout();
		} catch (Exception e) {
			// TODO:
		}
	}

	/*
	 * 
	 * 注册页面
	 */
	public void register() {
		// new UserApi().register("网可", "password");
	}
}
